package com.imcrl.piano.mvc.view
{
	import com.imcrl.piano.core.Assets;
	import com.imcrl.piano.core.NumberAssets;
	import com.imcrl.piano.mvc.model.MusicModel;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class ScoringView extends Sprite
	{
		private var perfectMc:MovieClip;
		private var comboMc:MovieClip;
		private var coolMc:MovieClip;
		private var goodMc:MovieClip;
		private var badMc:MovieClip;
		private var missMc:MovieClip;
		
		private var scoringIcons:Array;
		
		
		private var numberAssets:NumberAssets;
		
		//统计计分;
		private var perfectCount:int;
		private var coolCount:int;
		private var goodCount:int;
		private var badCount:int;
		private var missCount:int;
		private var combo:int;
		
		
		private var perfectScore:int = 200;
		private var coolScore:int = 150;
		private var goodScore:int = 100;
		private var badScore:int = 50;
		private var missScore:int = 0;
		
		//总数;
		private var totalScore:int = 0;
		public function ScoringView()
		{
			
			var mc:MovieClip;
			mc=perfectMc=new Assets.Perfect();
			mc.gotoAndStop(1);
			mc.visible=false;
			addChild(mc);
			
			mc=comboMc=new Assets.Combo();
			mc.gotoAndStop(1);
			mc.visible=false;
			addChild(mc);
			
			mc=coolMc=new Assets.Cool();
			mc.gotoAndStop(1);
			mc.visible=false;
			addChild(mc);
			
			mc=goodMc=new Assets.Good();
			mc.gotoAndStop(1);
			mc.visible=false;
			addChild(mc);
			
			mc=badMc=new Assets.Bad();
			mc.gotoAndStop(1);
			mc.visible=false;
			addChild(mc);
			
			mc=missMc=new Assets.Miss();
			mc.gotoAndStop(1);
			mc.visible=false;
			addChild(mc);
			
			
			scoringIcons = [comboMc,missMc,badMc,goodMc,coolMc,perfectMc];
			
			//输助定位;
			for each(mc in scoringIcons){
				mc.y=50;
				//mc.visible=true;
				//mc.gotoAndStop(mc.totalFrames/2);
			}
			
			
			numberAssets=new NumberAssets();
			numberAssets.viewNumber(100);
			this.addChild(numberAssets.getView());
		}
		
		public function clear():void{
			combo = 0
			totalScore = 0;
			
			perfectCount = 0;
			coolCount = 0;
			goodCount = 0;
			badCount = 0;
			missCount = 0;
		}
		
		
		private function perfect():void{
			//trace("perfect")
			perfectCount ++;
			combo ++;
			showIndex(5);
		} 
		
		private function cool():void{
			//trace("cool");
			coolCount ++;
			combo++;
			showIndex(4);
		}
		
		private function good():void{
			//trace("good")
			goodCount ++;
			combo++;
			showIndex(3);
		}
		
		private function bad():void{
			//trace("bad")
			badCount ++;
			combo = -1;
			showIndex(2);
		}
		
		private function miss():void{
			//trace("miss")
			missCount ++;
			combo = -1;
			showIndex(1);
		}
		
		public function check(value:int):void{
			var time:int=Math.abs(value);
			
			
			var souce:int;
			var isTrue:int = 0;
			var temp:int = (combo/50)
			temp = temp <0 ?  0 : temp;
			
			if(time<=MusicModel.perfectTime)
			{
				perfect();
				souce = perfectScore + temp*20;
			}
			else if(time<=MusicModel.coolTime)
			{
				cool();
				souce = coolScore + temp*15
			}
			else if(time<=MusicModel.goodTime)
			{
				good();
				souce = goodScore + temp*10;
			}
			else if(time<=MusicModel.badTime){
				bad();
				souce = badScore + temp*5;
			}else
			{
				miss();
				souce = missScore;
			}
			
			totalScore += souce;
			
			viewTotal(combo);
		}
		
		
		/**
		 * 显示积分 
		 * @param value;
		 * 
		 */		
		private function viewTotal(value:int):void{
			numberAssets.viewNumber(value);
		}
		
		
		public function showIndex(index:int):void{
			var mc:MovieClip;
			for each(mc in scoringIcons){
				mc.visible=false;
				mc.gotoAndStop(1);
			}
			
			mc=scoringIcons[index];
			mc.visible=true;
			mc.gotoAndPlay(1);
			mc.addEventListener(Event.ENTER_FRAME,enterHandler);
		}
		
		private function enterHandler(event:Event):void{
			var mc:MovieClip=event.currentTarget as MovieClip;
			if(mc.totalFrames==mc.currentFrame){
				mc.visible = false;
				mc.gotoAndStop(1);
				mc.removeEventListener(Event.ENTER_FRAME,enterHandler);
			}

		}
		
	}
}